package com.crazy.index.ftile;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class CharsetFilter
 */
@WebFilter(
		urlPatterns = { "*.do" }, 
		initParams = { 
				@WebInitParam(name = "encoding", value = "UTF-8")
		})
public final class CharsetFilter implements Filter {
	
	/**
	 * 编码
	 */
	private String encoding;
	
	public void destroy() {}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */                                                          
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// 编码
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		
		HttpServletRequest req = (HttpServletRequest) request;

		chain.doFilter(request, response);
	
	}
	public void init(FilterConfig fConfig) throws ServletException {
		this.encoding = fConfig.getInitParameter("encoding");
	}

}
